package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;
    //根据手机号查询会员信息
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    //新增会员
    public void add(Member member) {
        //如果用户设置了密码，需要对密码进行md5加密
        String password = member.getPassword();
        if(password != null){
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    //根据月份查询会员数量
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCounts = new ArrayList<>();
        if(months != null && months.size() > 0){
            for (String month : months) {//2019.01
                String endTime = month + ".31";
                Integer memberCount = memberDao.findMemberCountBeforeDate(endTime);
                memberCounts.add(memberCount);
            }
        }
        return memberCounts;
    }


    //男女会员人数占比
    @Override
    public Map memberGenderCount() {
        Map resultMap = new HashMap();
        List<String> genderList = new ArrayList<>();
        List<Map> genderCount = new ArrayList<>();
        List<Map> list = memberDao.findGenderCount();
        for (Map map : list) {
            Map temp = new HashMap();
            Integer gender = Integer.valueOf(map.get("sex").toString());
            if(gender==0){
                genderList.add("未知");
                temp.put("name","未知");
                temp.put("value",map.get("count"));
            }else if (gender==1){
                genderList.add("男");
                temp.put("name","男");
                temp.put("value",map.get("count"));
            }else if(gender==2){
                genderList.add("女");
                temp.put("name","女");
                temp.put("value",map.get("count"));
            }
            genderCount.add(temp);
        }
        resultMap.put("genderList",genderList);
        resultMap.put("genderCount",genderCount);
        return resultMap;
    }


    //各个年龄段会员人数占比
    @Override
    public Map memberAgeCount() {
        Map resultMap = new HashMap();
        List<String> ageList = new ArrayList<>();
        List<Map> ageCount = new ArrayList<>();
        List<Map> list = memberDao.findAgeCount();
        for (Map map : list) {
            Map temp = new HashMap();
            String age = String.valueOf(map.get("age"));
            if (age.equals("0-18")) {
                ageList.add("0-18岁");
                temp.put("name", "0-18岁");
                temp.put("value", map.get("count"));
            } else if (age.equals("18-30")) {
                ageList.add("18-30岁");
                temp.put("name", "18-30岁");
                temp.put("value", map.get("count"));
            }else if (age.equals("30-45")) {
                ageList.add("30-45岁");
                temp.put("name", "30-45岁");
                temp.put("value", map.get("count"));
            }else if (age.equals("45以上")) {
                ageList.add("45岁以上");
                temp.put("name", "45岁以上");
                temp.put("value", map.get("count"));
            }
            ageCount.add(temp);
        }
        resultMap.put("ageList", ageList);
        resultMap.put("ageCount", ageCount);
        return resultMap;
    }

}
